package lab03.test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

public class Test06 {
    public static void main(String[] args) {
        String path = "D:\\旅院\\作业\\java\\实验\\world.txt";
        RandomAccessFile file = null;

        Map<String, Integer> map = new HashMap<>();
        try {
            file = new RandomAccessFile(path, "r");
            String line = null;
            while ((line = file.readLine()) != null) {
                System.out.println(line);

                //先判断map中有没有这个key,没有直接将value赋值为1
                if (map.get(line) == null) {
                    map.put(line, 1);
                } else {
                    int value = map.get(line);
                    value += 1;
                    map.put(line, value);//方法一
                   // map.put(line, map.get(line) + 1); //方法二
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
